package com.coffee.controller;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.img.ImgUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import com.coffee.conmmon.BaseResult;
import com.coffee.conmmon.QRCodeUtil;
import com.coffee.entity.User;
import com.coffee.service.UserService;
import com.google.zxing.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Random;
import java.util.UUID;

/**
 * 2 * @Author: 刘佳乐 :面向百度编程
 * 3 * @Date: 2020/12/8 0:32
 * 4
 */
@RestController
@RequestMapping("/user")
@Api(description="用户相关接口")
@CrossOrigin
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 生成二维码
     */
    @GetMapping("/")
    public void productcode() {
        String s = UUID.randomUUID().toString();
        int i = new Random().nextInt(10);
        QRCodeUtil.zxingCodeCreate("www.baidu.com", "D:/voice/picture/2018/",500,"D:/voice/picture/2018/"+i+".jpg");
    }

    @RequestMapping(value = "/image1")
    public void productcodeimage(HttpServletResponse response) throws IOException {
        int i = new Random().nextInt(1000);
        String s = UUID.randomUUID().toString();
        BufferedImage bufferedImage = QRCodeUtil.getBufferedImage(s, 500, "D:/voice/picture/2018/"+i+".jpg");
        ImageIO.write(bufferedImage, "JPEG", response.getOutputStream());
    }
    @RequestMapping("/image")
    public String productcodeimage1(HttpServletResponse response) throws IOException {
        String s = UUID.randomUUID().toString();
        QrConfig config = new QrConfig(100, 100);
        // 设置边距，既二维码和背景之间的边距
        config.setMargin(1);
        // 设置前景色，既二维码颜色（青色）
        config.setForeColor(Color.BLACK.getRGB());
        // 设置背景色（灰色）
        config.setBackColor(Color.WHITE.getRGB());
        // 生成二维码到文件，也可以到流
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        QrCodeUtil.generate(s, config, ImgUtil.IMAGE_TYPE_PNG, outputStream);
        byte[] pngData = outputStream.toByteArray();
        // 这个前缀，可前端加，可后端加，不加的话，不能识别为图片
        return "data:image/png;base64," + Base64.encode(pngData);
    }
    /**
     * 解析二维码
     */
    @GetMapping("/test")
    public void analysiscode() {
        Result result = QRCodeUtil.zxingCodeAnalyze("D:/voice/picture/2018/759.jpg");
        System.err.println("二维码解析内容："+result.toString());
    }

    @RequestMapping(value = "/findAll",method = RequestMethod.GET)
    @ApiOperation("查找全部的接口")
    public BaseResult findAll(){
        return userService.findAll();
    }

    @RequestMapping(value = "login",method = RequestMethod.POST)
    public BaseResult login(@RequestBody User user, HttpServletRequest request){
        return userService.login(user,request);
    }

    @RequestMapping(value = "/sendMail",method = RequestMethod.GET)
    public BaseResult sendMail(@RequestParam("mail") String mail,HttpServletRequest request){
       return userService.sendMail(mail,request);
    }

    @RequestMapping(value = "/registry",method = RequestMethod.POST)
    public BaseResult registry(@RequestBody User user,HttpServletRequest request){
        return userService.registry(user,request);
    }

}
